﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;

namespace l1cs
{
    public class Logger
    {
        private static Logger _instance = new Logger();

        private Queue _queue = Queue.Synchronized( new Queue() );

        private Logger()
        {
            ThreadPool.QueueUserWorkItem( new WaitCallback( threadSub ) );
        }

        public void threadSub( object state )
        {
            string data;
            StreamWriter sw = new StreamWriter( "l1cs.log" );
            while ( true )
            {
                if ( _queue.Count != 0 )
                {
                    data = (string)_queue.Dequeue();

                    //Console.WriteLine( data );

                    sw.Write( DateTime.Now.ToString( "yyyy/MM/dd HH:mm:ss.fff" ) + " # " );
                    sw.WriteLine( data );
                    if ( !new Regex( "\n$" ).Match( data ).Success )
                        sw.Write( sw.NewLine );
                    sw.Flush();
                }
                Thread.Sleep( 100 );
            }
        }

        public void info( String str )
        {
            _queue.Enqueue( str );
        }

        public static Logger getLogger( String classname )
        {
            return _instance;
        }

        public void fine( String str )
        {
            info( str );
        }

        public void finest( String str )
        {
            info( str );
        }
        public void log( String flg, String str )
        {
            info( str );
        }
        public void config( String str )
        {
            info( str );
        }
        public void warning( String str )
        {
            info( str );
        }
        public void severe( String str )
        {
            info( str );
        }
        public void error( String str )
        {
            info( str );
        }
    }

    public class Level
    {
        public static String SEVERE = "SEVERE";
        public static String WARNING = "WARNING";
        public static String FINE = "FINE";
    }
}
